package com.ysp;

/**
 * 深克隆
 */

/**
 * @author ysp
 * @date 2021/1/9 15:05
 */
public class Apple implements Cloneable{

    private String name;
    private int size;
    private Address address;

    public Apple() {
    }

    public Apple(String name, int size, Address address) {
        this.name = name;
        this.size = size;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "name='" + name + '\'' +
                ", size=" + size +
                ", address=" + address +
                '}';
    }

    @Override
    protected Apple clone(){
        Apple apple = null;
        try {
            apple = (Apple) super.clone();
            //将引用属性进行深克隆
            apple.address = address.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return apple;
    }
}

/*
public class Apple implements Cloneable{

    private String name;
    private int size;
    private Address address;

    public Apple() {
    }

    public Apple(String name, int size, Address address) {
        this.name = name;
        this.size = size;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "name='" + name + '\'' +
                ", size=" + size +
                ", address=" + address +
                '}';
    }

    @Override
    protected Apple clone(){
        Apple apple = null;
        try {
            apple = (Apple) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return apple;
    }
}
 */
